package cn.cau.control;

import cn.cau.dao.AuditingApplyRecordsDao;
import cn.cau.dao.AuditingTopInfoDao;
import cn.cau.entity.*;
import cn.cau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class CodeAuditingPostController {
    /*
    编码审核
     */

    @Autowired
    private AuditingListService auditingListService;

    @Autowired
    private AuditingTopService auditingTopService;

    @Autowired
    private AuditingMidService auditingMidService;

    @Autowired
    private AuditingBotService auditingBotService;

    @Autowired
    private AuditingInfoService auditingInfoService;



    @RequestMapping(value = "/auditingTop",method = RequestMethod.POST)    //post方法接受top页面传递的整体审核信息
    public @ResponseBody String SaveTopAdvise(@RequestBody AuditingTopInfo auditingTopInfo){
        System.out.println("输出当前的info值如下：");
        System.out.println(auditingTopInfo);
        auditingTopService.addInfo(auditingTopInfo);
        return "success";
    }

    @RequestMapping(value = "/auditingMid",method = RequestMethod.POST)    //post方法接受Mid页面传递的整体审核信息
    public @ResponseBody String SaveMidAdvise(@RequestBody AuditingMidInfo auditingMidInfo){
        System.out.println("输出当前的info值如下：");
        System.out.println(auditingMidInfo);
        AuditingUtils util = new AuditingUtils();
        util.setCodeId(auditingMidInfo.getRuleId());
        util.setSegName(auditingMidInfo.getSegName());
        List<AuditingMidInfo> infos = auditingMidService.findAuditingMidInfoByCodeIdAndSegName(util);
        if(infos.size()!=0){
            //说明为更新操作
            auditingMidService.updateInfo(auditingMidInfo);
        }
        else{
            auditingMidService.AddNonEunmInfo(auditingMidInfo);
        }

        return "success";
    }

    @RequestMapping(value = "/auditingBot",method = RequestMethod.POST)    //post方法接受Bot页面传递的整体审核信息
    public  @ResponseBody String SaveBopAdvise(@RequestBody AuditingBotInfo auditingBotInfo){
        System.out.println("输出当前的info值如下：");
        System.out.println(auditingBotInfo);

        AuditingUtils utils = new AuditingUtils();
        utils.setSegName(auditingBotInfo.getSegName());
        utils.setCodeId(auditingBotInfo.getRuleId());



        List<AuditingBotInfo> infos = auditingBotService.findInfosByCodeIdAndSegName(utils);

        if(infos.size()!=0){
            auditingBotService.updateInfo(auditingBotInfo);
        }
        else{
            auditingBotService.AddBotInfo(auditingBotInfo);
        }

        return "success";
    }

    @RequestMapping(value = "/auditing",method = RequestMethod.POST)    //完成更新操作
    public  @ResponseBody String Update(@RequestBody AuditingResult auditingResult){
        System.out.println("输出审核结果如下：");
        System.out.println(auditingResult);
        //todo 更新副表和主表

        CodeManageRecord record = auditingInfoService.getRecordById(auditingResult.getRecordId());


        //更新manage表,置result=pass,
        if(auditingResult.getPass()==1){
            auditingResult.setPass(1);
            auditingInfoService.updateManageTable(auditingResult);
        }
        else{
            auditingResult.setPass(2);
            auditingInfoService.updateManageTable(auditingResult);
        }

        //对于修改审核
        if(auditingResult.getOp()==0){
            //更新coderule表，code_state = pass
            int new_id = record.getNewCodeRuleId();
            int old_id = record.getOldCodeRuleId();
            if(auditingResult.getPass()==1){
                //2代表通过审核
                auditingResult.setCodeRuleId(new_id);
                auditingResult.setPass(2);
                auditingInfoService.updateCodeTable(auditingResult);

                //废除旧的申请
                auditingResult.setCodeRuleId(old_id);
                auditingResult.setPass(4);
                auditingInfoService.updateCodeTable(auditingResult);
            }
            else{
                auditingResult.setRecordId(new_id);
                auditingResult.setPass(3);              //3代表未通过审核
                auditingInfoService.updateCodeTable(auditingResult);
            }
        }

        //对于废除审核
        else if(auditingResult.getOp()==1){
            //更新coderule表，code_state = pass
            if(auditingResult.getPass()==1){        //4代表已废除
                auditingResult.setCodeRuleId(record.getOldCodeRuleId());
                auditingResult.setPass(4);
                auditingInfoService.updateCodeTable(auditingResult);
            }
        }

        //对于设计审核
        else{
            if(auditingResult.getPass()==1){      //2代表通过审核
                auditingResult.setPass(2);
                auditingInfoService.updateCodeTable(auditingResult);
            }
            else{                               //3代表未通过审核
                auditingResult.setPass(3);
                auditingInfoService.updateCodeTable(auditingResult);
            }
        }

        return "success";
    }


}
